import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2;

/**
 * 接口字段维护 /ciam/role/api/:roleId
 * ApiField
 * @date 2019-07-09
 */
import React, { Component } from 'react';
import { routerRedux } from 'dva/router';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import queryString from 'query-string';
import { Content, Header } from 'components/Page';
import cacheComponent from 'components/CacheComponent';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import DataTable from "./DataTable";
import SearchForm from "./SearchForm";

function getFieldsValueByWrappedComponentRef(ref) {
  if (ref.current) {
    var form = ref.current.props.form;
    return form.getFieldsValue();
  }

  return {};
}

var ApiField = (_dec = connect(mapStateToProps, mapDispatchToProps), _dec2 = formatterCollections({
  code: ['ciam.roleManagement']
}), _dec3 = cacheComponent({
  cacheKey: '/ciam/role/api/list'
}), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(ApiField, _Component);

  var _super = _createSuper(ApiField);

  function ApiField(props) {
    var _this;

    _classCallCheck(this, ApiField);

    _this = _super.call(this, props);
    _this.state = {
      // 缓存的 分页
      cachePagination: {}
    };
    _this.searchFormRef = React.createRef();
    return _this;
  }

  _createClass(ApiField, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var apiInit = this.props.apiInit;
      apiInit();
      this.reload();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      var updateState = this.props.updateState;
      updateState({
        apiDataSource: [],
        apiPagination: {}
      });
    }
  }, {
    key: "reload",
    value: function reload() {
      var cachePagination = this.state.cachePagination;
      this.handleSearch(cachePagination);
    }
  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var pagination = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          queryApis = _this$props.queryApis,
          roleId = _this$props.match.params.roleId;
      this.setState({
        cachePagination: pagination
      });
      var params = getFieldsValueByWrappedComponentRef(this.searchFormRef);
      queryApis({
        params: _objectSpread(_objectSpread(_objectSpread({}, params), pagination), {}, {
          roleId: roleId
        })
      });
    } // SearchForm

  }, {
    key: "handleSearchFormSearch",
    value: function handleSearchFormSearch() {
      this.handleSearch();
    } // DataTable

  }, {
    key: "handleDataTableChange",
    value: function handleDataTableChange(page, filter, sort) {
      this.handleSearch({
        page: page,
        sort: sort
      });
    }
  }, {
    key: "handleApiFieldPermissionMaintain",
    value: function handleApiFieldPermissionMaintain(record) {
      var _this$props2 = this.props,
          pagePush = _this$props2.pagePush,
          search = _this$props2.location.search,
          roleId = _this$props2.match.params.roleId;

      var _queryString$parse = queryString.parse(search.substring(1)),
          fromSource = _queryString$parse.fromSource;

      var routerConfig = {
        pathname: "/ciam/role/field/".concat(roleId, "/").concat(record.id)
      };

      if (fromSource) {
        routerConfig = {
          pathname: "/ciam/role-tree/field/".concat(roleId, "/").concat(record.id),
          search: queryString.stringify({
            fromSource: fromSource
          })
        };
      }

      pagePush(routerConfig);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          dataSource = _this$props3.dataSource,
          pagination = _this$props3.pagination,
          loading = _this$props3.loading,
          requestMethod = _this$props3.requestMethod,
          search = _this$props3.location.search,
          path = _this$props3.match.path;

      var _queryString$parse2 = queryString.parse(search.substring(1)),
          fromSource = _queryString$parse2.fromSource,
          accessToken = _queryString$parse2.access_token;

      var backPath = path.indexOf('/private') === 0 ? fromSource ? "/private/ciam/role-tree?access_token=".concat(accessToken) : "/private/ciam/role?access_token=".concat(accessToken) : fromSource ? '/ciam/role-tree' : '/ciam/role';
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        backPath: backPath,
        title: intl.get('ciam.roleManagement.view.title.fieldPermission').d('接口字段权限配置')
      }), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(SearchForm, {
        onSearch: this.handleSearchFormSearch,
        requestMethod: requestMethod,
        wrappedComponentRef: this.searchFormRef
      }), /*#__PURE__*/React.createElement(DataTable, {
        path: path,
        dataSource: dataSource,
        pagination: pagination,
        loading: loading,
        onChange: this.handleDataTableChange,
        onApiFieldPermissionMaintain: this.handleApiFieldPermissionMaintain
      })));
    }
  }]);

  return ApiField;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "handleSearchFormSearch", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchFormSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDataTableChange", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDataTableChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleApiFieldPermissionMaintain", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleApiFieldPermissionMaintain"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { ApiField as default };

function mapStateToProps(_ref) {
  var roleManagement = _ref.roleManagement,
      loading = _ref.loading;
  var apiDataSource = roleManagement.apiDataSource,
      apiPagination = roleManagement.apiPagination,
      requestMethod = roleManagement.requestMethod;
  return {
    requestMethod: requestMethod,
    dataSource: apiDataSource,
    pagination: apiPagination,
    loading: loading.effects['roleManagement/apiInit'] || loading.effects['roleManagement/queryApis']
  };
}

function mapDispatchToProps(dispatch) {
  return {
    apiInit: function apiInit(payload) {
      return dispatch({
        type: 'roleManagement/apiInit',
        payload: payload
      });
    },
    queryApis: function queryApis(payload) {
      return dispatch({
        type: 'roleManagement/queryApis',
        payload: payload
      });
    },
    pagePush: function pagePush(payload) {
      dispatch(routerRedux.push(payload));
    },
    updateState: function updateState(payload) {
      dispatch({
        type: 'roleManagement/updateState',
        payload: payload
      });
    }
  };
}